<?php

namespace app\index\controller;

use think\Session;
use think\Controller;
use think\Db;
use think\Request;
use think\Loader;
use app\index\model\Inorder as OrderModel;


class Order extends Controller
{
	public function orders(OrderModel $order)
	{
		$uid=Session::get('uid');

    	$gnumber = Db::table('phone_inorder')->where(['userid'=>"$uid",'ddzt'=>'待支付'])->count('orderid');
    	if(!empty($gnumber)){
    	$this->assign('gnumber',$gnumber);
    	} else {
    		$this->assign('gnumber',0);
    	}
		$orde=$order->where(['userid'=>"$uid"])->select();
		$this->assign('data',$orde);
		return $this->fetch();
	}
	public function addres(OrderModel $order)
	{
		$uid=Session::get('uid');

    	$gnumber = Db::table('phone_inorder')->where(['userid'=>"$uid",'ddzt'=>'待支付'])->count('orderid');
    	if(!empty($gnumber)){
    	$this->assign('gnumber',$gnumber);
    	} else {
    		$this->assign('gnumber',0);
    	}
    	$gnumber = Db::table('phone_inorder')->where(['userid'=>"$uid"])->count('orderid');
    	if(!empty($gnumber)){
    	$this->assign('ber',$gnumber);
    	} else {
    		$this->assign('ber',0);
    	}
		//个性签名
		$res = Db::table('phone_user')->where('uid',"$uid")->value('qq');
		if(!empty($res)){
		$this->assign('userqq',$res);
		}
		//展示订单
		$orde=$order->where(['userid'=>"$uid"])->select();
		$this->assign('data',$orde);
		//dump($orde);
		$a=count($orde);
		//上传的头像
		$result = Db::table('phone_user')->where('uid',"$uid")->value('head');
		if(!empty($result)){
		$this->assign('uploadd',$result);
		}
		return $this->fetch();
	}
	public function home(OrderModel $order)
	{
		$uid=Session::get('uid');

    	$gnumber = Db::table('phone_inorder')->where(['userid'=>"$uid",'ddzt'=>'待支付'])->count('orderid');
    	if(!empty($gnumber)){
    	$this->assign('gnumber',$gnumber);
    	} else {
    		$this->assign('gnumber',0);
    	}
		$res = Db::table('phone_user')->where('uid',"$uid")->value('qq');
		$this->assign('userqq',$res);
		$re = Db::table('phone_user')->where('uid',"$uid")->value('phone');
		$this->assign('phone',$re);
		$resu = Db::table('phone_user')->where('uid',"$uid")->value('addr');
		$this->assign('addr',$resu);

		//过滤字段
		//$data->allowField(true)->save();
		$data = request()->post();
		//设置个人信息
		$us= request()->post('username');
		$ph= request()->post('phone');
		$ad= request()->post('addr');
		if(!empty($us)){
			$result = Db::table('phone_user')->where('uid',"$uid")->update(['qq'=>"$us",'phone'=>"$ph",'addr'=>"$ad"]);
		}

		//dump($us);

		if(!(null==(request()->file('image'))))
		{
		$file = request()->file('image');
		$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
		$upload = $info->getSaveName();
		Db::table('phone_user')->where('uid',"$uid")->update(['head'=>"$upload"]);
		//dump($upload);
		}

		$result = Db::table('phone_user')->where('uid',"$uid")->value('head');
		if(!empty($result)){
		$this->assign('uploadd',$result);
		} else {
			$this->assign('uploadd','null');
		}


		$orde=$order->where(['userid'=>"$uid"])->select();
		$this->assign('data',$orde);

		//dump($orde);
		$a=count($orde);

		return $this->fetch();
	}
	public function upload()
	{

  				//	获取表单上传文件	例如上传了001.jpg
  				$file = request()->file('image');
  				//	移动到框架应用根目录/public/uploads/	目录下
  				$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
  				if($info){
  								//	成功上传后	获取上传信息
  								//	输出	jpg
  								//echo $info->getExtension();
  								//	输出	20160820/42a79759f284b767dfcb2a0197904287.jpg
  								echo $info->getSaveName();
  								//	输出	42a79759f284b767dfcb2a0197904287.jpg
  								//echo $info->getFilename();
  				}else{
  								//	上传失败获取错误信息
  								echo $file->getError();
  				}

	}
	public function jiesuan()
	{
		$uid=Session::get('uid');
		$go = Db::table('phone_inorder')->where(['userid'=>"$uid",'ddzt'=>'待支付'])->select();
		$this->assign('go',$go);
    	$gnumber = Db::table('phone_inorder')->where(['userid'=>"$uid",'ddzt'=>'待支付'])->count('orderid');
    	if(!empty($gnumber)){
    	$this->assign('gnumber',$gnumber);
    	} else {
    		$this->assign('gnumber',0);
    	}
    	$go = Db::table('phone_inorder')->where(['userid'=>"$uid",'ddzt'=>'待支付'])->select();
		if(!empty($gnumber)){

		if($gnumber>=1){
		$mm=$go[0]['totalprice'];
		}
		//dump($mm);
		if($gnumber>=2){
		$mmm=$go[1]['totalprice'];
		$ccc=$mm+$mmm;
		}
		if($gnumber>=3){
		$mmmm=$go[2]['totalprice'];
		$cccc=$mmmm+$ccc;
		}
		if($gnumber>=4){
		$mmmmm=$go[3]['totalprice'];
		$ccccc=$mmmmm+$cccc;
		}
		if(!empty($ccccc)){
			$this->assign('price',$ccccc);
		} else if(!empty($cccc)){
			$this->assign('price',$cccc);
		} else if(!empty($ccc)){
			$this->assign('price',$ccc);
		} else if(!empty($mm)){
			$this->assign('price',$mm);
		}
			}
		return $this->fetch();
	}
	public function org()
	{
		$uid=Session::get('uid');
		Db::table('phone_inorder')->where(['userid'=>"$uid",'ddzt'=>'待支付'])->update(['ddzt'=>'已支付待发货']);
		$this->success('支付成功，请查看订单状态',THINK_URLC);
		//Db::table('phone_user')->where('uid',"$uid")->update(['qq'=>"$us",'phone'=>"$ph",'addr'=>"$ad"]);

	}
	public function ordersa(OrderModel $order)
	{
		$uid=Session::get('uid');

    	$gnumber = Db::table('phone_inorder')->where(['userid'=>"$uid",'ddzt'=>'待支付'])->count('orderid');
    	if(!empty($gnumber)){
    	$this->assign('gnumber',$gnumber);
    	} else {
    		$this->assign('gnumber',0);
    	}
		$orde=$order->where(['userid'=>"$uid",'ddzt'=>'已支付待发货'])->select();
		$this->assign('data',$orde);
		return $this->fetch();
	}
	public function ordersb(OrderModel $order)
	{
		$uid=Session::get('uid');

    	$gnumber = Db::table('phone_inorder')->where(['userid'=>"$uid",'ddzt'=>'待支付'])->count('orderid');
    	if(!empty($gnumber)){
    	$this->assign('gnumber',$gnumber);
    	} else {
    		$this->assign('gnumber',0);
    	}
		$orde=$order->where(['userid'=>"$uid",'ddzt'=>'待支付'])->select();
		$this->assign('data',$orde);
		return $this->fetch();
	}
	public function orderc(OrderModel $order)
	{
		$uid=Session::get('uid');

    	$gnumber = Db::table('phone_inorder')->where(['userid'=>"$uid",'ddzt'=>'待支付'])->count('orderid');
    	if(!empty($gnumber)){
    	$this->assign('gnumber',$gnumber);
    	} else {
    		$this->assign('gnumber',0);
    	}
		$orde=$order->where(['userid'=>"$uid",'ddzt'=>'已支付'])->select();
		$this->assign('data',$orde);
		return $this->fetch();
	}
	public function uppwd()
	{
		$uid=Session::get('uid');

    	$gnumber = Db::table('phone_inorder')->where(['userid'=>"$uid",'ddzt'=>'待支付'])->count('orderid');
    	if(!empty($gnumber)){
    	$this->assign('gnumber',$gnumber);
    	} else {
    		$this->assign('gnumber',0);
    	}
    	$uploadd=Db::name('user')->where('uid',$uid)->value('head');
    	//dump($uploadd);
    	$this->assign('uploadd',$uploadd);
    	if($_POST){
	    	//dump($_POST);
	    	$oldpwd=$_POST['oldpwd'];
	    	$newpwd=$_POST['newpwd'];
	    	$surepwd=$_POST['surepwd'];
	    	
	    	$oldpwd_db=Db::name('user')->where('uid',$uid)->value('password');
	    	//dump($oldpwd_db);
	    	//exit;
	    	if($oldpwd != $oldpwd_db){
	    		$this->success('原始密码错误！','Order/uppwd');
	    	}elseif($newpwd && $surepwd){
	    		if($newpwd == $surepwd){
	    			$update=Db::name('user')->where('uid',$uid)->update(['password'=>"$newpwd"]);
	    			$this->success('修改成功！','Order/uppwd');
	    		}else{
	    			$this->success('两次密码不一致！','Order/uppwd');
	    		}
	    	}else{
	    		$this->success('新密码或确认密码不能为空！','Order/uppwd');
	    	}
	    }
		return $this->fetch();
	}
}









